package org.example.leetCode;

/**
 * @author Administrator
 */
public class RemoveElements {
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        if (head.val == val) {
            if (head.next == null) {
                return null;
            }
            head = head.next;
        }
        ListNode temp = head;
        while (temp.next != null) {
            if (temp.next.val == val) {
                temp.next = temp.next.next;
            }
            temp = temp.next;
            if (temp == null) {
                break;
            }
        }
        return head;
    }
}
